<template>
	<view class="wrap">
		<view class="tHeader">
			<g-header title="已选商品" @rightclick="onDelTap">
				<image v-if="!isDelFlag" src="../../static/images/icon-del.png" style="width: 34rpx;height: 34rpx;">
				</image>
				<text v-else class="finished">完成</text>
			</g-header>
		</view>
		<view class="list" v-if="list.length > 0">
			<view class="itemWrap" v-for="(item, index) in list" :key="index">
				<view class="item">
					<view class="checkbox" v-if="isDelFlag">
						<u-checkbox v-model="item.checked" shape="circle" @change="onFirstCheckChange($event, index)">
						</u-checkbox>
					</view>
					<view class="item-img">
						<image :src="item.cover" mode=""></image>
					</view>
					<view class="item-main">
						<view class="item-title">{{item.productName}}</view>
						<view class="item-mainB">
							<view class="item-mainB_L">
								<text v-if="item.goodsType === 'dh'">￥{{item.salePrice}}/{{item.unit}}</text>
								<text v-if="item.goodsType === 'jy'">￥{{item.jyPrice}}/{{item.unit}}</text>
								<text>（{{item.emptyDiff}}）</text>
							</view>
							<view class="item-mainB_R">货号：{{item.id}}</view>
						</view>
					</view>
				</view>
				<view class="detail">
					<view class="detail-header" v-if="item.goodsType === 'dh'">大货</view>
					<view class="detail-header" v-if="item.goodsType === 'jy'">剪样</view>
					<view class="detail-main">
						<view class="detail-item" v-for="(val, idx) in item.colorList" :key="idx">
							<view class="app_flex-aic">
								<view class="checkbox" v-if="isDelFlag">
									<u-checkbox v-model="val.checked" shape="circle"
										@change="onSecondCheckChange($event, index, idx)"></u-checkbox>
								</view>
								<view class="detail-item_L">
									<view class="detail-item_img">
										<image :src="val.colorUrl" mode=""></image>
									</view>
									<view class="detail-item_title">
										<text>色号：</text>
										<text>{{`${val.colorCode + val.colorName}`}}</text>
									</view>
								</view>
							</view>
							<view class="detail-item_R">
								<template v-if="item.goodsType === 'dh'">
									<text>￥{{val.price}}</text>
									<text>x{{val.totalCount}}/{{item.unit}}</text>
								</template>
								<template v-if="item.goodsType === 'jy'">
									<text>￥{{val.jyPrice}}</text>
									<text>x{{val.count}}/{{item.unit}}</text>
								</template>
							</view>
						</view>
					</view>
				</view>
			</view>
			<no-more></no-more>
		</view>
		<no-data v-if="list.length == 0 && hasLoad"></no-data>
		<no-more v-if="page > 1 && noMore"></no-more>
		<p-chooseGoods-submitbar-del ref="submitbarDel" v-if="isDelFlag" @confirm="onDelConfirmTap"
			@change="onAllCheckedTap">
		</p-chooseGoods-submitbar-del>
		<p-chooseGoods-submitbar v-else :amount="totalPrice" @confirm="onConfirmTap"></p-chooseGoods-submitbar>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isDelFlag: false,
				list: [],
				page: 1,
				size: 15,
				hasLoad: false,
				noMore: false,
			}
		},
		computed: {
			totalPrice() {
				const price = this.list.reduce((total, item) => {
					return total + item.colorList.reduce((t, v) => {
						if (item.goodsType == 'dh') {
							return t + v.totalCount * v.price
						} else if (item.goodsType == 'jy') {
							return t + v.count * v.jyPrice
						} else {
							return t
						}
					}, 0)
				}, 0)
				return price.toFixed(2)
			}
		},
		onLoad(options) {
			this.updatePage()
		},
		onPullDownRefresh() {
			this.updatePage()
		},
		onReachBottom() {
			// this.page++
			// this.getList()
		},
		methods: {
			updatePage() {
				this.resetPage()
				this.getList()
			},
			resetPage() {
				this.list = []
				this.page = 1
				this.size = 15
				this.hasLoad = false
				this.noMore = false
			},
			onSkipTap(type) {
				const urlObj = {
					xxx: '/pages/xxx'
				}
				this.$g_util.navigationTo(urlObj, type)
			},
			onAllCheckedTap(flag) {
				this.list.forEach(item => {
					item.checked = flag
					item.colorList && item.colorList.forEach(val => {
						val.checked = flag
					})
				})
			},
			onDelTap() {
				this.isDelFlag = !this.isDelFlag
			},
			onFirstCheckChange({
				value
			}, index) {
				this.list[index].checked = value
				this.list[index].colorList?.forEach(item => {
					item.checked = value
				})
				this.$refs.submitbarDel.allCheckFlag = this.list.every(item => item.checked)
			},
			onSecondCheckChange(e, index, idx) {
				setTimeout(() => {
					let flag = this.list[index].colorList.every(item => {
						return item.checked
					})
					this.list[index].checked = flag
					this.$refs.submitbarDel.allCheckFlag = this.list.every(item => item.checked)
				}, 0)
			},
			getList() {
				if (this.gSalesTicketGoodsList.length > 0) {
					let list = this.$deepClone(this.gSalesTicketGoodsList)
					this.list = list.map(item => {
						item.checked = false
						if (item.colorList?.length > 0) {
							item.colorList = item.colorList.map(val => {
								val.checked = false
								return val
							})
						}
						return item
					})
				}
				this.hasLoad = true
			},
			onDelConfirmTap() {
				this.list = this.list.map(item => {
					item.colorList = item.colorList.filter(val => !val.checked)
					return item
				})
				this.list = this.list.filter(item => !item.checked && item.colorList.length > 0)
				this.$store.commit('updateSalesTicketGoodsList', this.$deepClone(this.list))
			},
			onConfirmTap() {

			},
		}
	}
</script>
<style>
	.u-checkbox {
		width: 50rpx;
	}
</style>
<style scoped lang="scss">
	.wrap {
		padding-top: calc(var(--status-bar-height) + 98rpx);
	}

	.tHeader {
		position: fixed;
		top: 0;
		left: 0;
		background-color: #fff;
		// padding-bottom: 20rpx;
		border-bottom: 1px solid $g-border-color;
		z-index: 10086;
	}

	.finished {
		font-size: 30rpx;
		font-family: FZLanTingHeiS-R-GB;
		font-weight: 400;
		color: #000000;
		line-height: 120rpx;
	}

	.list {
		padding-bottom: calc(100rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(100rpx + env(safe-area-inset-bottom));
	}

	.itemWrap {
		padding-bottom: 30rpx;
		border-bottom: 1px solid $g-border-color;
		background-color: #fff;
	}

	.item {
		display: flex;
		padding: 30rpx 20rpx 0;
	}

	.item-img {
		width: 140rpx;
		font-size: 0;

		image {
			width: 140rpx;
			height: 140rpx;
			border-radius: 10rpx;
			background-color: $g-main-bg;
		}
	}

	.item-main {
		flex: 1;
		margin-left: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.item-title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.item-mainB {
		display: flex;
		justify-content: space-between;
	}

	.item-mainB_L {
		text {
			&:first-child {
				color: #FF4C4C;
				font-size: 26rpx;
			}

			&:last-child {
				color: #999999;
				font-size: 20rpx;
			}
		}
	}

	.item-mainB_R {
		font-size: 24rpx;
		color: #999;
	}

	.detail {
		padding: 0 20rpx;
		background-color: #fff;
	}

	.detail-header {
		line-height: 70rpx;
	}

	.detail-main {
		padding: 20rpx;
		border-radius: 10rpx;
		background-color: #F7F7F7;
	}

	.detail-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		&:last-child {
			margin-bottom: 0;
		}
	}

	.detail-item_L {
		display: flex;
		align-items: center;
	}

	.detail-item_img {
		width: 50rpx;
		margin-right: 20rpx;
		font-size: 0;

		image {
			width: 50rpx;
			height: 50rpx;
			border-radius: 4rpx;
			background-color: skyblue;
		}
	}

	.detail-item_title {
		font-size: 24rpx;
		color: #333;
	}

	.detail-item_R {
		text {
			font-size: 24rpx;

			&:nth-child(1) {
				color: #FF4C4C;
			}

			&:nth-child(2) {
				color: #333;
			}
		}
	}

	.checkbox {
		width: 66rpx;
		display: flex;
		justify-content: center;
		overflow: hidden;
	}
</style>
